#!/usr/bin/perl
use strict;
use warnings;

my $numArgs = $#ARGV + 1;

if ($numArgs != 2) {
    print STDERR "\n";
    print STDERR "ERROR: comand line args, ($numArgs/2)\n";
    print STDERR "USAGE: fixOrigin.pl [DATA FILE] [OFFSET]\n";
    print STDERR "\n";
    exit(1);
}

my $DATA_FILE=$ARGV[0];

my $OFFSET=$ARGV[1];
print STDERR "DATA FILE = $DATA_FILE\n";
print STDERR "OFFSET    = $OFFSET\n";
print STDERR "---------------------------------------------\n";


my $OutFile = "$DATA_FILE-o$OFFSET.dat";

open(INDATA,"$DATA_FILE");
my @data = <INDATA>;
close(INDATA);
my $i=0;
#my $offsettime = 2450023.620000000000000000; ## E
#my $offsettime = 2443689.00000000000000;    ## Z
my $offsettime = $OFFSET;
open(OUTDATA,">$OutFile");
foreach my $line (@data) {
    my $AMP = 0.0;
    my $TIME = 0.0;
    chomp $line;
    my @Aline = split(/\s+/,$line);
    
 #   my $tmpL = length( $Aline[0] );

  #  print "$Aline[0]\n";
    if ($Aline[0]) {
#	print "<< $line\n";
	$AMP = $Aline[1];
	#print "A:$AMP\n";
	$TIME = $Aline[0]-$offsettime;
	#print "T:$TIME\n";
	#printf(">> %.4f  %.6f (%d)\n",$TIME,$AMP,$i); 
    printf(OUTDATA "%.3f  %.5f\n",$TIME,$AMP);
	#printf("%.4f  %.6f\n",$TIME,$AMP); 
	#print "= $Aline[0] $Aline[1] ($i)\n\n";
    }
    $i++;
}


close(OUTDATA);